from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import uuid
import time
from main.models import *
import requests
import PIL.Image as pillow

service = Service('./edgedriver/msedgedriver')
options = webdriver.EdgeOptions()
options.add_argument('--headless')
driver = webdriver.Edge(service=service,options=options)
# 无界面模式




def get_up_data(name,url):
    driver.get(url)
    time.sleep(2)
    fans = driver.find_element(by='xpath', value='/html/body/div[2]/div[2]/div/div[1]/div[3]/div[2]/p[2]').text ## 123.4万
    if fans.find('万') != -1:
            fans_num = int(float(fans.replace('万',''))*10000)
    else:
            fans_num = int(fans)
    up_name = driver.find_element(by='xpath', value='/html/body/div[2]/div[1]/div[1]/div[2]/div[2]/div/div[2]/div[1]/span[1]').text
    if up_name!= name:
        return False
    image_url = driver.find_element(by='xpath', value='/html/body/div[2]/div[1]/div[1]/div[2]/div[2]/div/div[1]/div/div/img').get_attribute('data-src')
    image_url = 'https:'+image_url.split('@')[0]
    image = requests.get(image_url)
    id = uuid.uuid4().hex
    with open('./static/picture/'+id+'.jpg','wb') as f:
        f.write(image.content)
    image_url = 'picture/'+id+'.jpg'
    image = pillow.open('./static/'+image_url)
    image.resize((500,500), pillow.ANTIALIAS)
    image.save('./static/'+image_url)
    up = Up(name=name,url=url,fans_num=fans_num,fans=fans,image=image_url,id=id,active=1,)
    up.save()

    driver.find_element(by='xpath', value='/html/body/div[2]/div[2]/div/div[1]/div[1]/a[3]').click()
    time.sleep(2)

    videos = []
    for i in range(1,11):
        url = driver.find_element(by='xpath', value='/html/body/div[2]/div[4]/div/div/div[2]/div[4]/div/div/ul[2]/li['+str(i)+']/a[1]').get_attribute('href')
        image_url = driver.find_element(by='xpath', value='/html/body/div[2]/div[4]/div/div/div[2]/div[4]/div/div/ul[2]/li['+str(i)+']/a[1]/div[1]/picture/img').get_attribute('src')
        image_url = image_url.split('@')[0]
        image = requests.get(image_url)
        video_id = uuid.uuid4().hex
        with open('./static/picture/'+video_id+'.jpg','wb') as f:
            f.write(image.content)
        image_path = 'picture/'+video_id+'.jpg'
        video = {
            'url': url,
            'id':video_id,
            'image':image_path,
            'image_url':image_url,
            'upid':id
        }
        videos.append(video)
    
    for video in videos:
        driver.get(video["url"])
        time.sleep(2)
        video["name"] = driver.find_element(by='xpath', value='/html/body/div[2]/div[2]/div[1]/div[1]/div[1]/div/h1').text
        video["date"] = driver.find_element(by='xpath', value='/html/body/div[2]/div[2]/div[1]/div[1]/div[2]/div/div[3]/div').text
        video["time"] = time.mktime(time.strptime(video["date"], "%Y-%m-%d %H:%M:%S"))
        views = driver.find_element(by='xpath', value='/html/body/div[2]/div[2]/div[1]/div[1]/div[2]/div/div[1]/div').text
        if views.find('万') != -1:
            views = int(float(views.replace('万',''))*10000)
        else:
            views = int(views)
        likes = driver.find_element(by='xpath', value='/html/body/div[2]/div[2]/div[1]/div[3]/div[1]/div[1]/div[1]/div/span').text
        if likes.find('万') != -1:
            likes = int(float(likes.replace('万',''))*10000)

        else:
            likes = int(likes)
        shares = driver.find_element(by='xpath', value='/html/body/div[2]/div[2]/div[1]/div[3]/div[1]/div[1]/div[4]/div/span/div[2]/div/span').text
        if shares.find('万') != -1:
            shares = int(float(shares.replace('万',''))*10000)
        else:
            shares = int(shares)
        collections = driver.find_element(by='xpath', value='/html/body/div[2]/div[2]/div[1]/div[3]/div[1]/div[1]/div[3]/div/span').text
        if collections.find('万') != -1:
            collections = int(float(collections.replace('万',''))*10000)
        else:
            collections = int(collections)
        video_data = {
            "videoid" : video["id"],
            "likes": likes,
            "date" :time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
            'time' : time.time(),
            "views": views,
            "shares": shares,
            "collections": collections,
        }
        Video.objects.create(**video)
        VideoData.objects.create(**video_data)


def get_video_image(videos):
    for video in videos:
        image = requests.get(video.image_url)
        with open('./static/picture/'+video.id+'.jpg','wb') as f:
            f.write(image.content)
        video.image = 'picture/'+video.id+'.jpg'
        video.save()